#include <stdio.h>
/*
 * 创建一个文件，写入Hello, World!
 */

 // 定义结构体 Student
struct Student {
    int id;          // 学生ID
    char name[20];   // 学生姓名
};

int main() {
    FILE *fp = fopen("data.txt", "r+");
    if (fp == NULL) return -1;
    struct Student student_to_write = { 1, "Alice" };
    fwrite(&student_to_write, sizeof(struct Student), 1, fp); // 写入结构体
    // 重置文件指针到文件开头，准备读取
    rewind(fp);
    struct Student student_to_read;
    while (fread(&student_to_read, sizeof(struct Student), 1, fp)) {
        printf("读取文件 %d %s\n", student_to_read.id, student_to_read.name);
    }
    fclose(fp);
    return 0;
}